package com.cx.controller.wjr;
/**
 * 温家饶
 */

import com.cx.pojo.Courier;
import com.cx.pojo.CourierExample;
import com.cx.service.wjr.CourierService;
import com.cx.vo.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("admin")
public class AdminCourierController {
    @Autowired
    CourierService courierService;


    @RequestMapping("addCourier")
    @ResponseBody
    public ResponseResult add(ResponseResult responseResult,Model model, Courier courier){
        System.out.println(courier);
        int courierNum =  courierService.addCourier(courier);
        if(courierNum > 0){
            responseResult.setCode(0);
            responseResult.setMessage("添加成功");
            return responseResult;
        }else{
            responseResult.setCode(1);
            responseResult.setMessage("添加失败");
            return responseResult;
        }
    }

    @RequestMapping("adminCourierList")
    @ResponseBody
    public ResponseResult list(@RequestParam(defaultValue = "1")Integer pageNum,Courier courier ,ResponseResult responseResult, CourierExample courierExample){
        PageHelper.startPage(pageNum, 5);
        System.out.println(courier);
        if(null != courier.getCourierPhone() && courier.getCourierPhone() != ""){
            courierExample.createCriteria().andCourierPhoneEqualTo(courier.getCourierPhone());
        }
        try {
            List<Courier> courierList =  courierService.selectByExample(courierExample);
            PageInfo pageInfo = new PageInfo(courierList,5);
            if(courierList.size()==0){
                responseResult.setCode(0);
            }else{
                responseResult.setCode(1);
            }
            responseResult.setData(pageInfo);
            System.out.println(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return responseResult;
    }

    @RequestMapping("adminUpdateCourier")
    @ResponseBody
    public ResponseResult update(ResponseResult responseResult,Model model,Courier courier, CourierExample courierExample){




        //courierExample.createCriteria().andCourierPhoneEqualTo("1234").andCourierAreaEqualTo("123");


        if(courier.getCourierPhone()!=null && courier.getCourierPhone()!=""){
            //selectMapper phone

        }
        if(courier.getCourierName()!=null && courier.getCourierName()!=""){
            courierExample.createCriteria().andCourierNameEqualTo(courier.getCourierName());
        }
        if(courier.getCourierNum()!=null){
            courierExample.createCriteria().andCourierNumEqualTo(courier.getCourierNum());
        }
        int courierList =  courierService.update(courier,courierExample);
        if(courierList>0){
            responseResult.setCode(1);
        }else{
            responseResult.setCode(0);
        }
        //courierList.forEach(System.out::println);
        responseResult.setData(courierList);
        return responseResult;
    }

    @RequestMapping("adminSelectCourier")
    @ResponseBody
    public ResponseResult select(ResponseResult responseResult, Model model, Courier courier, CourierExample courierExample){
        System.out.println(courier);



        List<Courier> courierNum = courierService.selectByExample(courierExample);
        courierNum.forEach(System.out::println);
        System.out.println(courierNum);
        responseResult.setData(courierNum);
        if(courierNum.size() > 0){
            responseResult.setMessage("查询成功");
            return responseResult;
        }
        responseResult.setMessage("查询失败");
        return responseResult;
    }

    @RequestMapping("adminCourierDelete")
    @ResponseBody
    public ResponseResult userDelete(ResponseResult responseResult,CourierExample courierExample,Courier courier , Model model){
        if(null != courier){
            int count =  courierService.delete(courierExample);
            if(count>0){
                responseResult.setMessage("删除成功");
                return responseResult;
            }else{
                responseResult.setMessage("删除失败");
                return responseResult;
            }
        }
        return responseResult;
    }

}